/**
 * Filename: NewsService.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/4 12:11
 */
package com.github.postsanf.yinian.service;
import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNNewsComment;
import com.github.postsanf.yinian.bean.YNNewsInvite;
import com.github.postsanf.yinian.bean.YNNewsNotice;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNNewsCommentResponse;
import com.github.postsanf.yinian.entity.YNNewsInviteResponse;
import com.github.postsanf.yinian.entity.YNNewsNoticeResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 消息后台服务
 */
public class NewsService extends BaseIntentService {

    public NewsService() {
        super("NewsService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            switch(action){
                // 获取邀请消息
                case ACTIONs.actionInviteNews:
                    int     iid   = intent.getExtras().getInt(ACTIONs.aNeedId);
                    String  itype = intent.getExtras().getString(ACTIONs.aNeedType);
                    getNewsInvite(iid,itype);
                    break;
                // 获取评论消息
                case ACTIONs.actionCommentNews:
                    int     cid   = intent.getExtras().getInt(ACTIONs.aNeedId);
                    String  ctype = intent.getExtras().getString(ACTIONs.aNeedType);
                    getNewsComment(cid,ctype);
                    break;
                // 获取通知消息
                case ACTIONs.actionNoticeNews:
                    int     nid   = intent.getExtras().getInt(ACTIONs.aNeedId);
                    String  ntype = intent.getExtras().getString(ACTIONs.aNeedType);
                    getNewsNotice(nid,ntype);
                    break;
            }
        }
    }

    // 获取邀请消息
    private void getNewsInvite(int needId , final String type)
    {
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_ID, String.valueOf(needId));
        mParam.put(CommonConstants.YNS_INFO_TYPE, type);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynGetNewsInvite))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        YNNewsInviteResponse resNewsInvite = gson.fromJson(s, YNNewsInviteResponse.class);
                        if (resNewsInvite!=null && ReqUtils.isSuccess(resNewsInvite.getCode()) ){
                            List<YNNewsInvite>  inviteLists = new ArrayList<>();
                            inviteLists.addAll(resNewsInvite.getData());
                            String direction  = ACTIONs.aAddBottom;
                            switch (type){
                                // 初始化
                                case CommonConstants.YNS_SIGN_INITS:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 加载
                                case CommonConstants.YNS_SIGN_LOAD:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 刷新
                                case CommonConstants.YNS_SIGN_REFRESH:
                                    direction = ACTIONs.aAddTop;
                                    break;
                            }
                            // 通知列表更新
                            Intent upIntent = new Intent(ACTIONs.actionInviteUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) inviteLists);
                            upIntent.putExtra(ACTIONs.aAddDirection,direction);
                            sendBroadcast(upIntent);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response){
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取评论消息
    private void getNewsComment(int needId , final String type)
    {
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_ID, String.valueOf(needId));
        mParam.put(CommonConstants.YNS_INFO_TYPE, type);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynGetNewsComment))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        Log.i("xjs","comment+ss===>"+s);
                        YNNewsCommentResponse resNewsCom = gson.fromJson(s, YNNewsCommentResponse.class);
                        if (resNewsCom!=null && ReqUtils.isSuccess(resNewsCom.getCode())){
                            List<YNNewsComment>  commentLists = new ArrayList<>();
                            commentLists.addAll(resNewsCom.getData());
                            String direction  = ACTIONs.aAddBottom;
                            switch (type){
                                // 初始化
                                case CommonConstants.YNS_SIGN_INITS:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 加载
                                case CommonConstants.YNS_SIGN_LOAD:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 刷新
                                case CommonConstants.YNS_SIGN_REFRESH:
                                    direction = ACTIONs.aAddTop;
                                    break;
                            }
                            // 通知列表更新
                            Intent upIntent = new Intent(ACTIONs.actionCommentUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) commentLists);
                            upIntent.putExtra(ACTIONs.aAddDirection,direction);
                            sendBroadcast(upIntent);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response){
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取通知消息
    private void getNewsNotice(int needId , final String type)
    {
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_ID, String.valueOf(needId));
        mParam.put(CommonConstants.YNS_INFO_TYPE, type);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynGetNewsNotice))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        YNNewsNoticeResponse resNewsNotice = gson.fromJson(s, YNNewsNoticeResponse.class);
                        if (resNewsNotice!=null && ReqUtils.isSuccess(resNewsNotice.getCode())){
                            List<YNNewsNotice>  noticeLists = new ArrayList<>();
                            noticeLists.addAll(resNewsNotice.getData());
                            String direction  = ACTIONs.aAddBottom;
                            switch (type){
                                // 初始化
                                case CommonConstants.YNS_SIGN_INITS:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 加载
                                case CommonConstants.YNS_SIGN_LOAD:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 刷新
                                case CommonConstants.YNS_SIGN_REFRESH:
                                    direction = ACTIONs.aAddTop;
                                    break;
                            }
                            // 通知列表更新
                            Intent upIntent = new Intent(ACTIONs.actionNoticeUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) noticeLists);
                            upIntent.putExtra(ACTIONs.aAddDirection,direction);
                            sendBroadcast(upIntent);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response){
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
